package com.zhangtai.modules.dao;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.entity.system.UserRoleEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 用户与角色对应关系
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-08-10 10:53:32
 */
@Mapper
public interface UserRoleDao extends BaseMapper<UserRoleEntity> {
	
	@Select("SELECT u.*,r.* FROM sys_user_role ur "
			+ "LEFT JOIN sys_user u ON ur.user_id = u.user_id "
			+ "LEFT JOIN sys_role r ON ur.role_id = r.role_id")
	public List<JSONObject> getUserRoleList(Page<JSONObject> page);
	
	@Delete("DELETE FROM sys_user_role WHERE user_id = #{userId}")
	public void deleteUserRole(@Param("userId") Long userId);

	@Update("UPDATE sys_user_role u set u.is_delete = 1 WHERE u.user_id = #{userId}")
	public List<UserRoleEntity> updateUserRole(@Param("userId") Long userId);

	@Select("SELECT ur.user_id,ur.role_id,ur.is_delete,r.role_name,r.name FROM sys_user_role ur "
			+ "LEFT JOIN sys_role r ON ur.role_id = r.role_id  where ur.user_id = #{userId} and ur.is_delete='0'")
	public List<JSONObject> getUserRoleById(@Param("userId") Long userId);
	
}
